package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.AnonymousComment;
import com.hpjpw.ido.domain.AnonymousMoment;
import com.hpjpw.ido.domain.MessageRemind;
import com.hpjpw.ido.service.IAnonymousCommentService;
import com.hpjpw.ido.service.IAnonymousMomentService;
import com.hpjpw.ido.service.IMessageRemindService;
import com.hpjpw.idoapp.service.IDoAnonymousService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ido/app/anonymousComment")
public class IDoAnonymousCommentController extends BaseController {
    @Autowired
    IAnonymousCommentService anonymousCommentService;
    @Autowired
    IDoAnonymousService anonymousService;
    @Autowired
    IAnonymousMomentService anonymousMomentService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    private IMessageRemindService messageRemindService;

    /**
     * 查询撕逼墙动态评论列表
     */
    @ApiOperation("查询撕逼墙动态评论列表，传入momentId(动态id)，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list/{momentId}")
    public TableDataInfo list(@PathVariable("momentId") Long momentId) {
        startPage();
        List<Map<String, String>> mapList = anonymousService.commentListResponse(momentId);
        return getDataTable(mapList);
    }

    /**
     * 新增撕逼墙动态的评论
     */
    @ApiOperation("新增撕逼墙动态的评论，接收momentId(动态id),content(评论内容)")
    @PostMapping()
    public AjaxResult addComment(@RequestBody AnonymousComment anonymousComment) {
        /* 安全校验1，无该动态 */
        if (StringUtils.isNull(anonymousMomentService.selectAnonymousMomentById(anonymousComment.getMomentId()))) {
            return AjaxResult.error("错误！没有此动态，请检查momentId！");
        }
        /* 安全校验2，无此评论者 */
        /*if(StringUtils.isNull(sysUserService.selectUserById(anonymousComment.getFromId()))){
            return AjaxResult.error("错误！没有此评论者，请检查fromId！");
        }*/
        /* 安全校验3，评论内容为空 */
        if (StringUtils.isNull(anonymousComment.getContent())) {
            return AjaxResult.error("错误！评论内容为空，请检查content！");
        }
        Long userId = SecurityUtils.getUserId();
        AnonymousComment anonymousCommentAdd = new AnonymousComment();
        anonymousCommentAdd.setMomentId(anonymousComment.getMomentId());
        anonymousCommentAdd.setContent(anonymousComment.getContent());
        anonymousCommentAdd.setFromId(userId);
        anonymousCommentAdd.setTime(new Date());
        anonymousCommentAdd.setToId(anonymousMomentService.selectAnonymousMomentById(anonymousComment.getMomentId()).getReportId());
        anonymousCommentService.insertAnonymousComment(anonymousCommentAdd);

        MessageRemind messageRemind = new MessageRemind(anonymousCommentAdd, SecurityUtils.getUsername());
        messageRemindService.insertMessageRemind(messageRemind);

        return AjaxResult.success();
    }

    /**
     * 删除撕逼墙评论
     */
    @ApiOperation("删除撕逼墙评论，接收momentId(动态id),id(评论id)")
    @DeleteMapping()
    public AjaxResult deleteComment(@RequestBody AnonymousComment anonymousComment) {
        /* 安全校验1，无此动态 */
        AnonymousMoment anonymousMomentCheck = anonymousMomentService.selectAnonymousMomentById(anonymousComment.getMomentId());
        if (StringUtils.isNull(anonymousMomentCheck)) {
            return AjaxResult.error("错误！无此动态，请检查momentId");
        }
        /* 安全校验2，无此评论 */
        AnonymousComment anonymousCommentCheck = anonymousCommentService.selectAnonymousCommentById(anonymousComment.getId());
        if (StringUtils.isNull(anonymousCommentCheck)) {
            return AjaxResult.error("错误！无此评论，请检查id");
        }
        /* 安全校验3，删除操作者非评论发布者 */
        Long userId = SecurityUtils.getUserId();
        if (!(anonymousCommentCheck.getFromId().equals(userId))) {
            return AjaxResult.error("错误！没有权限无法删除");
        }
        anonymousCommentService.deleteAnonymousCommentById(anonymousCommentCheck.getId());
        return AjaxResult.success();
    }
}
